

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.samplers._tpe.sampler &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">模块代码</a> &raquo;</li>
        
      <li>optuna.samplers._tpe.sampler</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.samplers._tpe.sampler 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">math</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">scipy.special</span>
<span class="kn">from</span> <span class="nn">scipy.stats</span> <span class="kn">import</span> <span class="n">truncnorm</span>

<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">distributions</span>
<span class="kn">from</span> <span class="nn">optuna.samplers._tpe.parzen_estimator</span> <span class="kn">import</span> <span class="n">_ParzenEstimator</span>
<span class="kn">from</span> <span class="nn">optuna.samplers._tpe.parzen_estimator</span> <span class="kn">import</span> <span class="n">_ParzenEstimatorParameters</span>
<span class="kn">from</span> <span class="nn">optuna.samplers</span> <span class="kn">import</span> <span class="n">BaseSampler</span>
<span class="kn">from</span> <span class="nn">optuna.samplers</span> <span class="kn">import</span> <span class="n">RandomSampler</span>
<span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">StudyDirection</span>
<span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">TrialState</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">type_checking</span>

<span class="k">if</span> <span class="n">type_checking</span><span class="o">.</span><span class="n">TYPE_CHECKING</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Callable</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Tuple</span>  <span class="c1"># NOQA</span>

    <span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">BaseDistribution</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">Study</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">FrozenTrial</span>  <span class="c1"># NOQA</span>

<span class="n">EPS</span> <span class="o">=</span> <span class="mf">1e-12</span>


<span class="k">def</span> <span class="nf">default_gamma</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="c1"># type: (int) -&gt; int</span>

    <span class="k">return</span> <span class="nb">min</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="mf">0.1</span> <span class="o">*</span> <span class="n">x</span><span class="p">)),</span> <span class="mi">25</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">hyperopt_default_gamma</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="c1"># type: (int) -&gt; int</span>

    <span class="k">return</span> <span class="nb">min</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="mf">0.25</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">))),</span> <span class="mi">25</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">default_weights</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="c1"># type: (int) -&gt; np.ndarray</span>

    <span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">([])</span>
    <span class="k">elif</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="mi">25</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">ramp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mf">1.0</span> <span class="o">/</span> <span class="n">x</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="n">x</span> <span class="o">-</span> <span class="mi">25</span><span class="p">)</span>
        <span class="n">flat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">ramp</span><span class="p">,</span> <span class="n">flat</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>


<div class="viewcode-block" id="TPESampler"><a class="viewcode-back" href="../../../../reference/samplers.html#optuna.samplers.TPESampler">[文档]</a><span class="k">class</span> <span class="nc">TPESampler</span><span class="p">(</span><span class="n">BaseSampler</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Sampler using TPE (Tree-structured Parzen Estimator) algorithm.</span>

<span class="sd">    This sampler is based on *independent sampling*.</span>
<span class="sd">    See also :class:`~optuna.samplers.BaseSampler` for more details of &#39;independent sampling&#39;.</span>

<span class="sd">    On each trial, for each parameter, TPE fits one Gaussian Mixture Model (GMM) ``l(x)`` to</span>
<span class="sd">    the set of parameter values associated with the best objective values, and another GMM</span>
<span class="sd">    ``g(x)`` to the remaining parameter values. It chooses the parameter value ``x`` that</span>
<span class="sd">    maximizes the ratio ``l(x)/g(x)``.</span>

<span class="sd">    For further information about TPE algorithm, please refer to the following papers:</span>

<span class="sd">    - `Algorithms for Hyper-Parameter Optimization</span>
<span class="sd">      &lt;https://papers.nips.cc/paper/4443-algorithms-for-hyper-parameter-optimization.pdf&gt;`_</span>
<span class="sd">    - `Making a Science of Model Search: Hyperparameter Optimization in Hundreds of</span>
<span class="sd">      Dimensions for Vision Architectures &lt;http://proceedings.mlr.press/v28/bergstra13.pdf&gt;`_</span>

<span class="sd">    Example:</span>

<span class="sd">        .. testcode::</span>

<span class="sd">            import optuna</span>
<span class="sd">            from optuna.samplers import TPESampler</span>

<span class="sd">            def objective(trial):</span>
<span class="sd">                x = trial.suggest_uniform(&#39;x&#39;, -10, 10)</span>
<span class="sd">                return x**2</span>

<span class="sd">            study = optuna.create_study(sampler=TPESampler())</span>
<span class="sd">            study.optimize(objective, n_trials=10)</span>

<span class="sd">    Args:</span>
<span class="sd">        consider_prior:</span>
<span class="sd">            Enhance the stability of Parzen estimator by imposing a Gaussian prior when</span>
<span class="sd">            :obj:`True`. The prior is only effective if the sampling distribution is</span>
<span class="sd">            either :class:`~optuna.distributions.UniformDistribution`,</span>
<span class="sd">            :class:`~optuna.distributions.DiscreteUniformDistribution`,</span>
<span class="sd">            :class:`~optuna.distributions.LogUniformDistribution`,</span>
<span class="sd">            :class:`~optuna.distributions.IntUniformDistribution`,</span>
<span class="sd">            or :class:`~optuna.distributions.IntLogUniformDistribution`.</span>
<span class="sd">        prior_weight:</span>
<span class="sd">            The weight of the prior. This argument is used in</span>
<span class="sd">            :class:`~optuna.distributions.UniformDistribution`,</span>
<span class="sd">            :class:`~optuna.distributions.DiscreteUniformDistribution`,</span>
<span class="sd">            :class:`~optuna.distributions.LogUniformDistribution`,</span>
<span class="sd">            :class:`~optuna.distributions.IntUniformDistribution`,</span>
<span class="sd">            :class:`~optuna.distributions.IntLogUniformDistribution`, and</span>
<span class="sd">            :class:`~optuna.distributions.CategoricalDistribution`.</span>
<span class="sd">        consider_magic_clip:</span>
<span class="sd">            Enable a heuristic to limit the smallest variances of Gaussians used in</span>
<span class="sd">            the Parzen estimator.</span>
<span class="sd">        consider_endpoints:</span>
<span class="sd">            Take endpoints of domains into account when calculating variances of Gaussians</span>
<span class="sd">            in Parzen estimator. See the original paper for details on the heuristics</span>
<span class="sd">            to calculate the variances.</span>
<span class="sd">        n_startup_trials:</span>
<span class="sd">            The random sampling is used instead of the TPE algorithm until the given number</span>
<span class="sd">            of trials finish in the same study.</span>
<span class="sd">        n_ei_candidates:</span>
<span class="sd">            Number of candidate samples used to calculate the expected improvement.</span>
<span class="sd">        gamma:</span>
<span class="sd">            A function that takes the number of finished trials and returns the number</span>
<span class="sd">            of trials to form a density function for samples with low grains.</span>
<span class="sd">            See the original paper for more details.</span>
<span class="sd">        weights:</span>
<span class="sd">            A function that takes the number of finished trials and returns a weight for them.</span>
<span class="sd">            See `Making a Science of Model Search: Hyperparameter Optimization in Hundreds of</span>
<span class="sd">            Dimensions for Vision Architectures &lt;http://proceedings.mlr.press/v28/bergstra13.pdf&gt;`_</span>
<span class="sd">            for more details.</span>
<span class="sd">        seed:</span>
<span class="sd">            Seed for random number generator.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">consider_prior</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>  <span class="c1"># type: bool</span>
        <span class="n">prior_weight</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span>  <span class="c1"># type: float</span>
        <span class="n">consider_magic_clip</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>  <span class="c1"># type: bool</span>
        <span class="n">consider_endpoints</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>  <span class="c1"># type: bool</span>
        <span class="n">n_startup_trials</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>  <span class="c1"># type: int</span>
        <span class="n">n_ei_candidates</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span>  <span class="c1"># type: int</span>
        <span class="n">gamma</span><span class="o">=</span><span class="n">default_gamma</span><span class="p">,</span>  <span class="c1"># type: Callable[[int], int]</span>
        <span class="n">weights</span><span class="o">=</span><span class="n">default_weights</span><span class="p">,</span>  <span class="c1"># type: Callable[[int], np.ndarray]</span>
        <span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[int]</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_parzen_estimator_parameters</span> <span class="o">=</span> <span class="n">_ParzenEstimatorParameters</span><span class="p">(</span>
            <span class="n">consider_prior</span><span class="p">,</span> <span class="n">prior_weight</span><span class="p">,</span> <span class="n">consider_magic_clip</span><span class="p">,</span> <span class="n">consider_endpoints</span><span class="p">,</span> <span class="n">weights</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_prior_weight</span> <span class="o">=</span> <span class="n">prior_weight</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_n_startup_trials</span> <span class="o">=</span> <span class="n">n_startup_trials</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_n_ei_candidates</span> <span class="o">=</span> <span class="n">n_ei_candidates</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_gamma</span> <span class="o">=</span> <span class="n">gamma</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_weights</span> <span class="o">=</span> <span class="n">weights</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_rng</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">RandomState</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_random_sampler</span> <span class="o">=</span> <span class="n">RandomSampler</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="n">seed</span><span class="p">)</span>

<div class="viewcode-block" id="TPESampler.reseed_rng"><a class="viewcode-back" href="../../../../reference/samplers.html#optuna.samplers.TPESampler.reseed_rng">[文档]</a>    <span class="k">def</span> <span class="nf">reseed_rng</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_rng</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">RandomState</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_random_sampler</span><span class="o">.</span><span class="n">reseed_rng</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">infer_relative_search_space</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">):</span>
        <span class="c1"># type: (Study, FrozenTrial) -&gt; Dict[str, BaseDistribution]</span>

        <span class="k">return</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">sample_relative</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">search_space</span><span class="p">):</span>
        <span class="c1"># type: (Study, FrozenTrial, Dict[str, BaseDistribution]) -&gt; Dict[str, Any]</span>

        <span class="k">return</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">sample_independent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_distribution</span><span class="p">):</span>
        <span class="c1"># type: (Study, FrozenTrial, str, BaseDistribution) -&gt; Any</span>

        <span class="n">values</span><span class="p">,</span> <span class="n">scores</span> <span class="o">=</span> <span class="n">_get_observation_pairs</span><span class="p">(</span><span class="n">study</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">trial</span><span class="p">)</span>

        <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_n_startup_trials</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_random_sampler</span><span class="o">.</span><span class="n">sample_independent</span><span class="p">(</span>
                <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_distribution</span>
            <span class="p">)</span>
        <span class="n">below_param_values</span><span class="p">,</span> <span class="n">above_param_values</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_split_observation_pairs</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">scores</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">param_distribution</span><span class="p">,</span> <span class="n">distributions</span><span class="o">.</span><span class="n">UniformDistribution</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_uniform</span><span class="p">(</span><span class="n">param_distribution</span><span class="p">,</span> <span class="n">below_param_values</span><span class="p">,</span> <span class="n">above_param_values</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">param_distribution</span><span class="p">,</span> <span class="n">distributions</span><span class="o">.</span><span class="n">LogUniformDistribution</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_loguniform</span><span class="p">(</span>
                <span class="n">param_distribution</span><span class="p">,</span> <span class="n">below_param_values</span><span class="p">,</span> <span class="n">above_param_values</span>
            <span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">param_distribution</span><span class="p">,</span> <span class="n">distributions</span><span class="o">.</span><span class="n">DiscreteUniformDistribution</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_discrete_uniform</span><span class="p">(</span>
                <span class="n">param_distribution</span><span class="p">,</span> <span class="n">below_param_values</span><span class="p">,</span> <span class="n">above_param_values</span>
            <span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">param_distribution</span><span class="p">,</span> <span class="n">distributions</span><span class="o">.</span><span class="n">IntUniformDistribution</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_int</span><span class="p">(</span><span class="n">param_distribution</span><span class="p">,</span> <span class="n">below_param_values</span><span class="p">,</span> <span class="n">above_param_values</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">param_distribution</span><span class="p">,</span> <span class="n">distributions</span><span class="o">.</span><span class="n">IntLogUniformDistribution</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_int_loguniform</span><span class="p">(</span>
                <span class="n">param_distribution</span><span class="p">,</span> <span class="n">below_param_values</span><span class="p">,</span> <span class="n">above_param_values</span>
            <span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">param_distribution</span><span class="p">,</span> <span class="n">distributions</span><span class="o">.</span><span class="n">CategoricalDistribution</span><span class="p">):</span>
            <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_categorical_index</span><span class="p">(</span>
                <span class="n">param_distribution</span><span class="p">,</span> <span class="n">below_param_values</span><span class="p">,</span> <span class="n">above_param_values</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="n">param_distribution</span><span class="o">.</span><span class="n">choices</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">distribution_list</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">distributions</span><span class="o">.</span><span class="n">UniformDistribution</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="n">distributions</span><span class="o">.</span><span class="n">LogUniformDistribution</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="n">distributions</span><span class="o">.</span><span class="n">DiscreteUniformDistribution</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="n">distributions</span><span class="o">.</span><span class="n">IntUniformDistribution</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="n">distributions</span><span class="o">.</span><span class="n">IntLogUniformDistribution</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="n">distributions</span><span class="o">.</span><span class="n">CategoricalDistribution</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">]</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
                <span class="s2">&quot;The distribution </span><span class="si">{}</span><span class="s2"> is not implemented. &quot;</span>
                <span class="s2">&quot;The parameter distribution should be one of the </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">param_distribution</span><span class="p">,</span> <span class="n">distribution_list</span>
                <span class="p">)</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_split_observation_pairs</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">config_vals</span><span class="p">,</span>  <span class="c1"># type: List[Optional[float]]</span>
        <span class="n">loss_vals</span><span class="p">,</span>  <span class="c1"># type: List[Tuple[float, float]]</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; Tuple[np.ndarray, np.ndarray]</span>

        <span class="n">config_vals</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">config_vals</span><span class="p">)</span>
        <span class="n">loss_vals</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">loss_vals</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="p">[(</span><span class="s2">&quot;step&quot;</span><span class="p">,</span> <span class="nb">float</span><span class="p">),</span> <span class="p">(</span><span class="s2">&quot;score&quot;</span><span class="p">,</span> <span class="nb">float</span><span class="p">)])</span>

        <span class="n">n_below</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gamma</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">config_vals</span><span class="p">))</span>
        <span class="n">loss_ascending</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argsort</span><span class="p">(</span><span class="n">loss_vals</span><span class="p">)</span>
        <span class="n">below</span> <span class="o">=</span> <span class="n">config_vals</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">loss_ascending</span><span class="p">[:</span><span class="n">n_below</span><span class="p">])]</span>
        <span class="n">below</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">([</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">below</span> <span class="k">if</span> <span class="n">v</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">)</span>
        <span class="n">above</span> <span class="o">=</span> <span class="n">config_vals</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">loss_ascending</span><span class="p">[</span><span class="n">n_below</span><span class="p">:])]</span>
        <span class="n">above</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">([</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">above</span> <span class="k">if</span> <span class="n">v</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span>

    <span class="k">def</span> <span class="nf">_sample_uniform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">distribution</span><span class="p">,</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span><span class="p">):</span>
        <span class="c1"># type: (distributions.UniformDistribution, np.ndarray, np.ndarray) -&gt; float</span>

        <span class="n">low</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span>
        <span class="n">high</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">high</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_numerical</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_sample_loguniform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">distribution</span><span class="p">,</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span><span class="p">):</span>
        <span class="c1"># type: (distributions.LogUniformDistribution, np.ndarray, np.ndarray) -&gt; float</span>

        <span class="n">low</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span>
        <span class="n">high</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">high</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_numerical</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span><span class="p">,</span> <span class="n">is_log</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_sample_discrete_uniform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">distribution</span><span class="p">,</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span><span class="p">):</span>
        <span class="c1"># type:(distributions.DiscreteUniformDistribution, np.ndarray, np.ndarray) -&gt; float</span>

        <span class="n">q</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">q</span>
        <span class="n">r</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span>
        <span class="c1"># [low, high] is shifted to [0, r] to align sampled values at regular intervals.</span>
        <span class="n">low</span> <span class="o">=</span> <span class="mi">0</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">q</span>
        <span class="n">high</span> <span class="o">=</span> <span class="n">r</span> <span class="o">+</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">q</span>

        <span class="c1"># Shift below and above to [0, r]</span>
        <span class="n">above</span> <span class="o">-=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span>
        <span class="n">below</span> <span class="o">-=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span>

        <span class="n">best_sample</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_numerical</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span><span class="p">,</span> <span class="n">q</span><span class="o">=</span><span class="n">q</span><span class="p">)</span> <span class="o">+</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span>
        <span class="k">return</span> <span class="nb">min</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">best_sample</span><span class="p">,</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">),</span> <span class="n">distribution</span><span class="o">.</span><span class="n">high</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_sample_int</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">distribution</span><span class="p">,</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span><span class="p">):</span>
        <span class="c1"># type: (distributions.IntUniformDistribution, np.ndarray, np.ndarray) -&gt; int</span>

        <span class="n">d</span> <span class="o">=</span> <span class="n">distributions</span><span class="o">.</span><span class="n">DiscreteUniformDistribution</span><span class="p">(</span>
            <span class="n">low</span><span class="o">=</span><span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">distribution</span><span class="o">.</span><span class="n">high</span><span class="p">,</span> <span class="n">q</span><span class="o">=</span><span class="n">distribution</span><span class="o">.</span><span class="n">step</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_sample_discrete_uniform</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_sample_int_loguniform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">distribution</span><span class="p">,</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span><span class="p">):</span>
        <span class="c1"># type: (distributions.IntLogUniformDistribution, np.ndarray, np.ndarray) -&gt; int</span>

        <span class="n">low</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span> <span class="o">-</span> <span class="mf">0.5</span>
        <span class="n">high</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">high</span> <span class="o">+</span> <span class="mf">0.5</span>

        <span class="n">sample</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_numerical</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span><span class="p">,</span> <span class="n">is_log</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">best_sample</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">((</span><span class="n">sample</span> <span class="o">-</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">/</span> <span class="n">distribution</span><span class="o">.</span><span class="n">step</span><span class="p">)</span> <span class="o">*</span> <span class="n">distribution</span><span class="o">.</span><span class="n">step</span>
            <span class="o">+</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">best_sample</span><span class="p">,</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">),</span> <span class="n">distribution</span><span class="o">.</span><span class="n">high</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_sample_numerical</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">low</span><span class="p">,</span>  <span class="c1"># type: float</span>
        <span class="n">high</span><span class="p">,</span>  <span class="c1"># type: float</span>
        <span class="n">below</span><span class="p">,</span>  <span class="c1"># type: np.ndarray</span>
        <span class="n">above</span><span class="p">,</span>  <span class="c1"># type: np.ndarray</span>
        <span class="n">q</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[float]</span>
        <span class="n">is_log</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>  <span class="c1"># type: bool</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; float</span>

        <span class="k">if</span> <span class="n">is_log</span><span class="p">:</span>
            <span class="n">low</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">low</span><span class="p">)</span>
            <span class="n">high</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">high</span><span class="p">)</span>
            <span class="n">below</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">below</span><span class="p">)</span>
            <span class="n">above</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">above</span><span class="p">)</span>

        <span class="n">size</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_n_ei_candidates</span><span class="p">,)</span>

        <span class="n">parzen_estimator_below</span> <span class="o">=</span> <span class="n">_ParzenEstimator</span><span class="p">(</span>
            <span class="n">mus</span><span class="o">=</span><span class="n">below</span><span class="p">,</span> <span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_parzen_estimator_parameters</span>
        <span class="p">)</span>
        <span class="n">samples_below</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_from_gmm</span><span class="p">(</span>
            <span class="n">parzen_estimator</span><span class="o">=</span><span class="n">parzen_estimator_below</span><span class="p">,</span> <span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">,</span> <span class="n">q</span><span class="o">=</span><span class="n">q</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">size</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">log_likelihoods_below</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gmm_log_pdf</span><span class="p">(</span>
            <span class="n">samples</span><span class="o">=</span><span class="n">samples_below</span><span class="p">,</span>
            <span class="n">parzen_estimator</span><span class="o">=</span><span class="n">parzen_estimator_below</span><span class="p">,</span>
            <span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span>
            <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">,</span>
            <span class="n">q</span><span class="o">=</span><span class="n">q</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="n">parzen_estimator_above</span> <span class="o">=</span> <span class="n">_ParzenEstimator</span><span class="p">(</span>
            <span class="n">mus</span><span class="o">=</span><span class="n">above</span><span class="p">,</span> <span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_parzen_estimator_parameters</span>
        <span class="p">)</span>

        <span class="n">log_likelihoods_above</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_gmm_log_pdf</span><span class="p">(</span>
            <span class="n">samples</span><span class="o">=</span><span class="n">samples_below</span><span class="p">,</span>
            <span class="n">parzen_estimator</span><span class="o">=</span><span class="n">parzen_estimator_above</span><span class="p">,</span>
            <span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span>
            <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">,</span>
            <span class="n">q</span><span class="o">=</span><span class="n">q</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="n">ret</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span>
            <span class="n">TPESampler</span><span class="o">.</span><span class="n">_compare</span><span class="p">(</span>
                <span class="n">samples</span><span class="o">=</span><span class="n">samples_below</span><span class="p">,</span> <span class="n">log_l</span><span class="o">=</span><span class="n">log_likelihoods_below</span><span class="p">,</span> <span class="n">log_g</span><span class="o">=</span><span class="n">log_likelihoods_above</span>
            <span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span> <span class="k">if</span> <span class="n">is_log</span> <span class="k">else</span> <span class="n">ret</span>

    <span class="k">def</span> <span class="nf">_sample_categorical_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">distribution</span><span class="p">,</span> <span class="n">below</span><span class="p">,</span> <span class="n">above</span><span class="p">):</span>
        <span class="c1"># type: (distributions.CategoricalDistribution, np.ndarray, np.ndarray) -&gt; int</span>

        <span class="n">choices</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">choices</span>
        <span class="n">below</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">below</span><span class="p">))</span>
        <span class="n">above</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">above</span><span class="p">))</span>
        <span class="n">upper</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">choices</span><span class="p">)</span>
        <span class="n">size</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_n_ei_candidates</span><span class="p">,)</span>

        <span class="n">weights_below</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_weights</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">below</span><span class="p">))</span>
        <span class="n">counts_below</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">bincount</span><span class="p">(</span><span class="n">below</span><span class="p">,</span> <span class="n">minlength</span><span class="o">=</span><span class="n">upper</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="n">weights_below</span><span class="p">)</span>
        <span class="n">weighted_below</span> <span class="o">=</span> <span class="n">counts_below</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prior_weight</span>
        <span class="n">weighted_below</span> <span class="o">/=</span> <span class="n">weighted_below</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
        <span class="n">samples_below</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample_from_categorical_dist</span><span class="p">(</span><span class="n">weighted_below</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>
        <span class="n">log_likelihoods_below</span> <span class="o">=</span> <span class="n">TPESampler</span><span class="o">.</span><span class="n">_categorical_log_pdf</span><span class="p">(</span><span class="n">samples_below</span><span class="p">,</span> <span class="n">weighted_below</span><span class="p">)</span>

        <span class="n">weights_above</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_weights</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">above</span><span class="p">))</span>
        <span class="n">counts_above</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">bincount</span><span class="p">(</span><span class="n">above</span><span class="p">,</span> <span class="n">minlength</span><span class="o">=</span><span class="n">upper</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="n">weights_above</span><span class="p">)</span>
        <span class="n">weighted_above</span> <span class="o">=</span> <span class="n">counts_above</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prior_weight</span>
        <span class="n">weighted_above</span> <span class="o">/=</span> <span class="n">weighted_above</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
        <span class="n">log_likelihoods_above</span> <span class="o">=</span> <span class="n">TPESampler</span><span class="o">.</span><span class="n">_categorical_log_pdf</span><span class="p">(</span><span class="n">samples_below</span><span class="p">,</span> <span class="n">weighted_above</span><span class="p">)</span>

        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span>
            <span class="n">TPESampler</span><span class="o">.</span><span class="n">_compare</span><span class="p">(</span>
                <span class="n">samples</span><span class="o">=</span><span class="n">samples_below</span><span class="p">,</span> <span class="n">log_l</span><span class="o">=</span><span class="n">log_likelihoods_below</span><span class="p">,</span> <span class="n">log_g</span><span class="o">=</span><span class="n">log_likelihoods_above</span>
            <span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_sample_from_gmm</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">parzen_estimator</span><span class="p">,</span>  <span class="c1"># type: _ParzenEstimator</span>
        <span class="n">low</span><span class="p">,</span>  <span class="c1"># type: float</span>
        <span class="n">high</span><span class="p">,</span>  <span class="c1"># type: float</span>
        <span class="n">q</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[float]</span>
        <span class="n">size</span><span class="o">=</span><span class="p">(),</span>  <span class="c1"># type: Tuple</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; np.ndarray</span>

        <span class="n">weights</span> <span class="o">=</span> <span class="n">parzen_estimator</span><span class="o">.</span><span class="n">weights</span>
        <span class="n">mus</span> <span class="o">=</span> <span class="n">parzen_estimator</span><span class="o">.</span><span class="n">mus</span>
        <span class="n">sigmas</span> <span class="o">=</span> <span class="n">parzen_estimator</span><span class="o">.</span><span class="n">sigmas</span>
        <span class="n">weights</span><span class="p">,</span> <span class="n">mus</span><span class="p">,</span> <span class="n">sigmas</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">,</span> <span class="p">(</span><span class="n">weights</span><span class="p">,</span> <span class="n">mus</span><span class="p">,</span> <span class="n">sigmas</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">low</span> <span class="o">&gt;=</span> <span class="n">high</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The &#39;low&#39; should be lower than the &#39;high&#39;. &quot;</span>
                <span class="s2">&quot;But (low, high) = (</span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">).&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="n">active</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_rng</span><span class="o">.</span><span class="n">multinomial</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">weights</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">size</span><span class="p">),</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">trunc_low</span> <span class="o">=</span> <span class="p">(</span><span class="n">low</span> <span class="o">-</span> <span class="n">mus</span><span class="p">[</span><span class="n">active</span><span class="p">])</span> <span class="o">/</span> <span class="n">sigmas</span><span class="p">[</span><span class="n">active</span><span class="p">]</span>
        <span class="n">trunc_high</span> <span class="o">=</span> <span class="p">(</span><span class="n">high</span> <span class="o">-</span> <span class="n">mus</span><span class="p">[</span><span class="n">active</span><span class="p">])</span> <span class="o">/</span> <span class="n">sigmas</span><span class="p">[</span><span class="n">active</span><span class="p">]</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">samples</span> <span class="o">=</span> <span class="n">truncnorm</span><span class="o">.</span><span class="n">rvs</span><span class="p">(</span>
                <span class="n">trunc_low</span><span class="p">,</span>
                <span class="n">trunc_high</span><span class="p">,</span>
                <span class="n">size</span><span class="o">=</span><span class="n">size</span><span class="p">,</span>
                <span class="n">loc</span><span class="o">=</span><span class="n">mus</span><span class="p">[</span><span class="n">active</span><span class="p">],</span>
                <span class="n">scale</span><span class="o">=</span><span class="n">sigmas</span><span class="p">[</span><span class="n">active</span><span class="p">],</span>
                <span class="n">random_state</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_rng</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">samples</span> <span class="o">&lt;</span> <span class="n">high</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">():</span>
                <span class="k">break</span>

        <span class="k">if</span> <span class="n">q</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">samples</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">samples</span> <span class="o">/</span> <span class="n">q</span><span class="p">)</span> <span class="o">*</span> <span class="n">q</span>

    <span class="k">def</span> <span class="nf">_gmm_log_pdf</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">samples</span><span class="p">,</span>  <span class="c1"># type: np.ndarray</span>
        <span class="n">parzen_estimator</span><span class="p">,</span>  <span class="c1"># type: _ParzenEstimator</span>
        <span class="n">low</span><span class="p">,</span>  <span class="c1"># type: float</span>
        <span class="n">high</span><span class="p">,</span>  <span class="c1"># type: float</span>
        <span class="n">q</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[float]</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; np.ndarray</span>

        <span class="n">weights</span> <span class="o">=</span> <span class="n">parzen_estimator</span><span class="o">.</span><span class="n">weights</span>
        <span class="n">mus</span> <span class="o">=</span> <span class="n">parzen_estimator</span><span class="o">.</span><span class="n">mus</span>
        <span class="n">sigmas</span> <span class="o">=</span> <span class="n">parzen_estimator</span><span class="o">.</span><span class="n">sigmas</span>
        <span class="n">samples</span><span class="p">,</span> <span class="n">weights</span><span class="p">,</span> <span class="n">mus</span><span class="p">,</span> <span class="n">sigmas</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">,</span> <span class="p">(</span><span class="n">samples</span><span class="p">,</span> <span class="n">weights</span><span class="p">,</span> <span class="n">mus</span><span class="p">,</span> <span class="n">sigmas</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">samples</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">([],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">weights</span><span class="o">.</span><span class="n">ndim</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The &#39;weights&#39; should be 2-dimension. &quot;</span>
                <span class="s2">&quot;But weights.shape = </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">weights</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">mus</span><span class="o">.</span><span class="n">ndim</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The &#39;mus&#39; should be 2-dimension. But mus.shape = </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">mus</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">sigmas</span><span class="o">.</span><span class="n">ndim</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The &#39;sigmas&#39; should be 2-dimension. But sigmas.shape = </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sigmas</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="n">p_accept</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span>
            <span class="n">weights</span>
            <span class="o">*</span> <span class="p">(</span>
                <span class="n">TPESampler</span><span class="o">.</span><span class="n">_normal_cdf</span><span class="p">(</span><span class="n">high</span><span class="p">,</span> <span class="n">mus</span><span class="p">,</span> <span class="n">sigmas</span><span class="p">)</span>
                <span class="o">-</span> <span class="n">TPESampler</span><span class="o">.</span><span class="n">_normal_cdf</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">mus</span><span class="p">,</span> <span class="n">sigmas</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="p">)</span>

        <span class="k">if</span> <span class="n">q</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">distance</span> <span class="o">=</span> <span class="n">samples</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="kc">None</span><span class="p">]</span> <span class="o">-</span> <span class="n">mus</span>
            <span class="n">mahalanobis</span> <span class="o">=</span> <span class="p">(</span><span class="n">distance</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">sigmas</span><span class="p">,</span> <span class="n">EPS</span><span class="p">))</span> <span class="o">**</span> <span class="mi">2</span>
            <span class="n">Z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span> <span class="o">*</span> <span class="n">sigmas</span>
            <span class="n">coefficient</span> <span class="o">=</span> <span class="n">weights</span> <span class="o">/</span> <span class="n">Z</span> <span class="o">/</span> <span class="n">p_accept</span>
            <span class="k">return</span> <span class="n">TPESampler</span><span class="o">.</span><span class="n">_logsum_rows</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">mahalanobis</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">coefficient</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cdf_func</span> <span class="o">=</span> <span class="n">TPESampler</span><span class="o">.</span><span class="n">_normal_cdf</span>
            <span class="n">upper_bound</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">samples</span> <span class="o">+</span> <span class="n">q</span> <span class="o">/</span> <span class="mf">2.0</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
            <span class="n">lower_bound</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">samples</span> <span class="o">-</span> <span class="n">q</span> <span class="o">/</span> <span class="mf">2.0</span><span class="p">,</span> <span class="n">low</span><span class="p">)</span>
            <span class="n">probabilities</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span>
                <span class="n">weights</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="kc">None</span><span class="p">]</span>
                <span class="o">*</span> <span class="p">(</span>
                    <span class="n">cdf_func</span><span class="p">(</span><span class="n">upper_bound</span><span class="p">[</span><span class="kc">None</span><span class="p">],</span> <span class="n">mus</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="kc">None</span><span class="p">],</span> <span class="n">sigmas</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="kc">None</span><span class="p">])</span>
                    <span class="o">-</span> <span class="n">cdf_func</span><span class="p">(</span><span class="n">lower_bound</span><span class="p">[</span><span class="kc">None</span><span class="p">],</span> <span class="n">mus</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="kc">None</span><span class="p">],</span> <span class="n">sigmas</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="kc">None</span><span class="p">])</span>
                <span class="p">),</span>
                <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">probabilities</span> <span class="o">+</span> <span class="n">EPS</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">p_accept</span> <span class="o">+</span> <span class="n">EPS</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_sample_from_categorical_dist</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">probabilities</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span>
        <span class="c1"># type: (np.ndarray, Tuple[int]) -&gt; np.ndarray</span>

        <span class="k">if</span> <span class="n">probabilities</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">probabilities</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">):</span>
            <span class="n">probabilities</span> <span class="o">=</span> <span class="n">probabilities</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">probabilities</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">probabilities</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">size</span> <span class="o">==</span> <span class="p">(</span><span class="mi">0</span><span class="p">,):</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">([],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">)</span>
        <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">size</span><span class="p">)</span>
        <span class="k">assert</span> <span class="n">probabilities</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">1</span>

        <span class="n">n_draws</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">(</span><span class="n">size</span><span class="p">))</span>
        <span class="n">sample</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_rng</span><span class="o">.</span><span class="n">multinomial</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">pvals</span><span class="o">=</span><span class="n">probabilities</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">n_draws</span><span class="p">))</span>
        <span class="k">assert</span> <span class="n">sample</span><span class="o">.</span><span class="n">shape</span> <span class="o">==</span> <span class="n">size</span> <span class="o">+</span> <span class="p">(</span><span class="n">probabilities</span><span class="o">.</span><span class="n">size</span><span class="p">,)</span>
        <span class="n">return_val</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">probabilities</span><span class="o">.</span><span class="n">size</span><span class="p">))</span>
        <span class="n">return_val</span><span class="o">.</span><span class="n">shape</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">return</span> <span class="n">return_val</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_categorical_log_pdf</span><span class="p">(</span>
        <span class="bp">cls</span><span class="p">,</span>
        <span class="n">sample</span><span class="p">,</span>  <span class="c1"># type: np.ndarray</span>
        <span class="n">p</span><span class="p">,</span>  <span class="c1"># type: np.ndarray</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; np.ndarray</span>

        <span class="k">if</span> <span class="n">sample</span><span class="o">.</span><span class="n">size</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">p</span><span class="p">)[</span><span class="n">sample</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">([])</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_compare</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">samples</span><span class="p">,</span> <span class="n">log_l</span><span class="p">,</span> <span class="n">log_g</span><span class="p">):</span>
        <span class="c1"># type: (np.ndarray, np.ndarray, np.ndarray) -&gt; np.ndarray</span>

        <span class="n">samples</span><span class="p">,</span> <span class="n">log_l</span><span class="p">,</span> <span class="n">log_g</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">,</span> <span class="p">(</span><span class="n">samples</span><span class="p">,</span> <span class="n">log_l</span><span class="p">,</span> <span class="n">log_g</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">samples</span><span class="o">.</span><span class="n">size</span><span class="p">:</span>
            <span class="n">score</span> <span class="o">=</span> <span class="n">log_l</span> <span class="o">-</span> <span class="n">log_g</span>
            <span class="k">if</span> <span class="n">samples</span><span class="o">.</span><span class="n">size</span> <span class="o">!=</span> <span class="n">score</span><span class="o">.</span><span class="n">size</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;The size of the &#39;samples&#39; and that of the &#39;score&#39; &quot;</span>
                    <span class="s2">&quot;should be same. &quot;</span>
                    <span class="s2">&quot;But (samples.size, score.size) = (</span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">samples</span><span class="o">.</span><span class="n">size</span><span class="p">,</span> <span class="n">score</span><span class="o">.</span><span class="n">size</span><span class="p">)</span>
                <span class="p">)</span>

            <span class="n">best</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">score</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">([</span><span class="n">samples</span><span class="p">[</span><span class="n">best</span><span class="p">]]</span> <span class="o">*</span> <span class="n">samples</span><span class="o">.</span><span class="n">size</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">([])</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_logsum_rows</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="c1"># type: (np.ndarray) -&gt; np.ndarray</span>

        <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="n">m</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="n">m</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">])</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span> <span class="o">+</span> <span class="n">m</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_normal_cdf</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">):</span>
        <span class="c1"># type: (float, np.ndarray, np.ndarray) -&gt; np.ndarray</span>

        <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">,</span> <span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">))</span>
        <span class="n">denominator</span> <span class="o">=</span> <span class="n">x</span> <span class="o">-</span> <span class="n">mu</span>
        <span class="n">numerator</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">EPS</span><span class="p">)</span>
        <span class="n">z</span> <span class="o">=</span> <span class="n">denominator</span> <span class="o">/</span> <span class="n">numerator</span>
        <span class="k">return</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">scipy</span><span class="o">.</span><span class="n">special</span><span class="o">.</span><span class="n">erf</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_log_normal_cdf</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">):</span>
        <span class="c1"># type: (float, np.ndarray, np.ndarray) -&gt; np.ndarray</span>

        <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">,</span> <span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Negative argument is given to _lognormal_cdf. x: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
        <span class="n">denominator</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">EPS</span><span class="p">))</span> <span class="o">-</span> <span class="n">mu</span>
        <span class="n">numerator</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">EPS</span><span class="p">)</span>
        <span class="n">z</span> <span class="o">=</span> <span class="n">denominator</span> <span class="o">/</span> <span class="n">numerator</span>
        <span class="k">return</span> <span class="mf">0.5</span> <span class="o">+</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">scipy</span><span class="o">.</span><span class="n">special</span><span class="o">.</span><span class="n">erf</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>

<div class="viewcode-block" id="TPESampler.hyperopt_parameters"><a class="viewcode-back" href="../../../../reference/samplers.html#optuna.samplers.TPESampler.hyperopt_parameters">[文档]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">hyperopt_parameters</span><span class="p">():</span>
        <span class="c1"># type: () -&gt; Dict[str, Any]</span>
        <span class="sd">&quot;&quot;&quot;Return the the default parameters of hyperopt (v0.1.2).</span>

<span class="sd">        :class:`~optuna.samplers.TPESampler` can be instantiated with the parameters returned</span>
<span class="sd">        by this method.</span>

<span class="sd">        Example:</span>

<span class="sd">            Create a :class:`~optuna.samplers.TPESampler` instance with the default</span>
<span class="sd">            parameters of `hyperopt &lt;https://github.com/hyperopt/hyperopt/tree/0.1.2&gt;`_.</span>

<span class="sd">            .. testcode::</span>

<span class="sd">                    import optuna</span>
<span class="sd">                    from optuna.samplers import TPESampler</span>

<span class="sd">                    def objective(trial):</span>
<span class="sd">                        x = trial.suggest_uniform(&#39;x&#39;, -10, 10)</span>
<span class="sd">                        return x**2</span>

<span class="sd">                    sampler = TPESampler(**TPESampler.hyperopt_parameters())</span>
<span class="sd">                    study = optuna.create_study(sampler=sampler)</span>
<span class="sd">                    study.optimize(objective, n_trials=10)</span>

<span class="sd">        Returns:</span>
<span class="sd">            A dictionary containing the default parameters of hyperopt.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="p">{</span>
            <span class="s2">&quot;consider_prior&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
            <span class="s2">&quot;prior_weight&quot;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span>
            <span class="s2">&quot;consider_magic_clip&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
            <span class="s2">&quot;consider_endpoints&quot;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
            <span class="s2">&quot;n_startup_trials&quot;</span><span class="p">:</span> <span class="mi">20</span><span class="p">,</span>
            <span class="s2">&quot;n_ei_candidates&quot;</span><span class="p">:</span> <span class="mi">24</span><span class="p">,</span>
            <span class="s2">&quot;gamma&quot;</span><span class="p">:</span> <span class="n">hyperopt_default_gamma</span><span class="p">,</span>
            <span class="s2">&quot;weights&quot;</span><span class="p">:</span> <span class="n">default_weights</span><span class="p">,</span>
        <span class="p">}</span></div></div>


<span class="k">def</span> <span class="nf">_get_observation_pairs</span><span class="p">(</span><span class="n">study</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">trial</span><span class="p">):</span>
    <span class="c1"># type: (Study, str, FrozenTrial) -&gt; Tuple[List[Optional[float]], List[Tuple[float, float]]]</span>
    <span class="sd">&quot;&quot;&quot;Get observation pairs from the study.</span>

<span class="sd">       This function collects observation pairs from the complete or pruned trials of the study.</span>
<span class="sd">       The values for trials that don&#39;t contain the parameter named ``param_name`` are set to None.</span>

<span class="sd">       An observation pair fundamentally consists of a parameter value and an objective value.</span>
<span class="sd">       However, due to the pruning mechanism of Optuna, final objective values are not always</span>
<span class="sd">       available. Therefore, this function uses intermediate values in addition to the final</span>
<span class="sd">       ones, and reports the value with its step count as ``(-step, value)``.</span>
<span class="sd">       Consequently, the structure of the observation pair is as follows:</span>
<span class="sd">       ``(param_value, (-step, value))``.</span>

<span class="sd">       The second element of an observation pair is used to rank observations in</span>
<span class="sd">       ``_split_observation_pairs`` method (i.e., observations are sorted lexicographically by</span>
<span class="sd">       ``(-step, value)``).</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">sign</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="k">if</span> <span class="n">study</span><span class="o">.</span><span class="n">direction</span> <span class="o">==</span> <span class="n">StudyDirection</span><span class="o">.</span><span class="n">MAXIMIZE</span><span class="p">:</span>
        <span class="n">sign</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>

    <span class="n">values</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">scores</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">trial</span> <span class="ow">in</span> <span class="n">study</span><span class="o">.</span><span class="n">_storage</span><span class="o">.</span><span class="n">get_all_trials</span><span class="p">(</span><span class="n">study</span><span class="o">.</span><span class="n">_study_id</span><span class="p">,</span> <span class="n">deepcopy</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span> <span class="ow">is</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">COMPLETE</span> <span class="ow">and</span> <span class="n">trial</span><span class="o">.</span><span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">score</span> <span class="o">=</span> <span class="p">(</span><span class="o">-</span><span class="nb">float</span><span class="p">(</span><span class="s2">&quot;inf&quot;</span><span class="p">),</span> <span class="n">sign</span> <span class="o">*</span> <span class="n">trial</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span> <span class="ow">is</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">PRUNED</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">intermediate_values</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">step</span><span class="p">,</span> <span class="n">intermediate_value</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">intermediate_values</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
                <span class="k">if</span> <span class="n">math</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">intermediate_value</span><span class="p">):</span>
                    <span class="n">score</span> <span class="o">=</span> <span class="p">(</span><span class="o">-</span><span class="n">step</span><span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="s2">&quot;inf&quot;</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">score</span> <span class="o">=</span> <span class="p">(</span><span class="o">-</span><span class="n">step</span><span class="p">,</span> <span class="n">sign</span> <span class="o">*</span> <span class="n">intermediate_value</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">score</span> <span class="o">=</span> <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s2">&quot;inf&quot;</span><span class="p">),</span> <span class="mf">0.0</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">continue</span>

        <span class="n">param_value</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># type: Optional[float]</span>
        <span class="k">if</span> <span class="n">param_name</span> <span class="ow">in</span> <span class="n">trial</span><span class="o">.</span><span class="n">params</span><span class="p">:</span>
            <span class="n">distribution</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">distributions</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span>
            <span class="n">param_value</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">to_internal_repr</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">param_name</span><span class="p">])</span>

        <span class="n">values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">param_value</span><span class="p">)</span>
        <span class="n">scores</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">score</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">values</span><span class="p">,</span> <span class="n">scores</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>